<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogFormVisible"
    width="500px"
    @close="close"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="100px">
      <el-form-item label="姓名" prop="supplier_name">
        <el-input v-model="form.supplier_name"  :disabled="is_disabled"/>
      </el-form-item>
      <el-form-item label="手机号" prop="mobile">
        <el-input v-model="form.mobile" :disabled="is_disabled"/>
      </el-form-item>
      <el-form-item label="供应商别名" prop="supplier_alias">
        <el-input v-model="form.supplier_alias" />
      </el-form-item>
      <!--
        分类可能属于多个
        内部: supplier_category_ids: [1,2]
        接口: supplier_category_id: '1,2'
      -->
      <!--<el-form-item label="类别" prop="supplier_category_ids">
        <el-select
          v-model="form.supplier_category_ids"
          placeholder="请选择供应商类别"
          multiple
          filterable
          clearable
        >
          <el-option
            v-for="item in supplierCategories"
            :key="item.id"
            :label="item.category_name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>-->
      <el-form-item label="身份证号" prop="idcard">
        <el-input v-model="form.idcard" :disabled="is_disabled"/>
      </el-form-item>
     <!-- <el-form-item label="银行卡号" prop="bank_card">
        <el-input v-model="form.bank_card" />
      </el-form-item>
      <el-form-item label="开户行" prop="bank">
        <el-input v-model="form.bank" />
      </el-form-item>-->
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { getDetail, doAdd, doEdit } from '@/api/supplier'

  export default {
    name: 'SupplierEdit',

    props: {
      supplierCategories: {
        type: Array,
        required: true,
        default: () => [],
      },
    },

    data() {
      return {
        form: {
          id: '',
          supplier_name: '',
          mobile: '',
          supplier_alias: '',
          //supplier_category_ids: [], // 内部
          //supplier_category_id: '', // 接口
          idcard: '',
          //bank_card: '',
         // bank: '',
          //idcard_img: '',
          //bank_card_img: '',
        },
        rules: {
          supplier_name: [
            { required: true, trigger: 'blur', message: '请输入姓名' },
          ],
          mobile: [
            { required: true, trigger: 'blur', message: '请输入手机号' },
          ],
          supplier_alias: [
            { required: true, trigger: 'blur', message: '请输入供应商别名' },
          ],
          supplier_category_ids: [
            { required: true, trigger: 'change', message: '请选择供应商分类' },
          ],
          idcard: [
            { required: true, trigger: 'blur', message: '请输入身份证号' },
          ],
          bank_card: [
            { required: true, trigger: 'blur', message: '请输入银行卡号' },
          ],
          bank: [
            { required: true, trigger: 'blur', message: '请输入银行名称' },
          ],
        },
        title: '',
        dialogFormVisible: false,
        type: '',
        is_disabled:false,
      }
    },

    watch: {
      // 修改分类时, 同步至 supplier_category_id
      'form.supplier_category_ids'(ids) {
        this.form.supplier_category_id = ids.join()
      },
    },

    created() {},

    methods: {
      initDetail() {
        getDetail(this.form.id).then((res) => {
          this.form = {
            ...res,
            supplier_id: res.id,
            supplier_category_ids: res.supplier_category_id
              .split(',')
              .map((id) => +id),
          }
        })
      },
      showEdit(row) {
        if (!row) {
          this.title = '添加'
          this.type = 'add'
          this.is_disabled = false
        } else {
          this.title = '编辑'
          this.type = 'edit'
          this.is_disabled = true
          this.form.id = row.id
          this.initDetail()
        }
        this.dialogFormVisible = true
      },
      close() {
        this.$refs['form'].resetFields()
        this.form = this.$options.data().form
        this.dialogFormVisible = false
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            let msg
            if (this.type === 'add') msg = await doAdd(this.form)
            else msg = await doEdit(this.form)
            this.$baseMessage(msg, 'success')
            this.$emit('fetch-data')
            this.close()
          } else {
            return false
          }
        })
      },
    },
  }
</script>
